﻿

Zepto(function ($) {
    var pi = 1;
    var orderby = 5;
    var cid = 0;

    FastClick.attach(document.body);
    $(".nb-canvas .am-icon-close").click(function () {
        $(".nb-sharecanvas").css("display", "none");
    });
           
    //排序
    $('.mcp ul li').click(function () {
        orderby = $(this).attr('data-type');
        $(this).siblings().removeClass("active");
        $(this).addClass("active");
        get_list(1);
    })
    //分类
    $('#globalNav a').click(function ()
    {
        $("#search_input").val("");
        cid = $(this).attr('data-type');
        $('#globalNav .bline').remove();
        $(this).parent().append('<span class="bline"></span>');
        get_list(1);
    })
    $("#bnt_search").click(function () {
        if ($.trim($("#search_input").val()) != "")
        {
            $('#globalNav .bline').remove();
            cid = 0;
            get_list(1);
        }
    })
    $("#search_input").on('keypress', function (e)
    {
        var _this = $(this);
        if (e.keyCode == 13 && $.trim(_this.val()) != '')
        {
            $('#globalNav .bline').remove();
            cid = 0;
            get_list(1);
        }
    });


    Util.lazyLoad('lazy');
     
    get_list(0);
    var aid = $("#aid").val();
    function get_list(ty) {

        if (ty == 1) {
            pi = 1;
            $("#couponlist").html('');
        }
        $.ajax({
            type: "post",
            url: "/u/getDLProduct.html",
            data: {
                pi: pi,
                orderby: orderby,
                cid: cid,
                key: $("#search_input").val()
            },
            dataType: "json",
            success: function (data)
            {
                if (data.State == true && data.total > 0)
                {
                    var list = data.rows.CurrentPageList;
                    var content = '';
                    for (var i = 0; i < list.length; i++)
                    {
                        var d = list[i];
                        content += " <li>";
                        content += '<a class="lt34 clearfix"  onclick="ProductPop(' + d.auctionId + ')">';
                        content += '<div class="spic lazy new"  data-original="' +d.pictUrl + '_400x400"></div>';
                        content += ' <div class="param">';
                        if (list[i].userType == 2) {
                            content += '<h5 class="icon_taobao">' + d.title + '</h5>';
                        }
                        else {
                            content += '<h5 class="icon_tmall">' + d.title + '</h5>';
                        }
                        content += ' <div class="price">';
                        content += '<p class="oprice">￥' +d.zkPrice + '</p>';
                        content += '<p class="dprice">券后价￥<strong>' + (d.zkPrice - d.couponAmount).toFixed(1) + '</strong></p>';
                        content += ' </div>';
                        content += '<div class="sdate"><p style="font-size: 0.29rem; color: #ff6562;">佣金<strong style="font-size: 22px; padding-left: 2px;color: #EF422D; line-height: 20px;font-family:georgia,sans-serif; vertical-align: bottom; ">' + d.tkRate + '</strong></p></div>';
                        content += '</div>';
                        content += '<div class="appcoupon"><span class="fl plus">优惠券' + d.couponAmount + '元</span><span class="fr opt">推广</span></div>';
                        content += ' </a> </li>';
                        //<span class="fl couponlistspan">券<i>' + d.couponAmount + '元</i></span>
                    }
                    $("#couponlist").append(content);
                    Util.lazyLoad('lazy.new');
                    $('.lazy.new').removeClass('new');
                    pi++;
                } else
                {
                    $(".bbn").html('<span>没有更多记录！</span>');
                          
                }
            },
            error: function (xhr, type) {

            }
        });
               
              
    }
           
          

    function getScrollTop() {
        var scrollTop = 0;
        if (document.documentElement && document.documentElement.scrollTop) {
            scrollTop = document.documentElement.scrollTop;
        }
        else if (document.body) {
            scrollTop = document.body.scrollTop;
        }
        return scrollTop;
    }
    function getClientHeight() {
        var clientHeight = 0;
        if (document.body.clientHeight && document.documentElement.clientHeight) {
            clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight);
        }
        else {
            clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight);
        }
        return clientHeight;
    }

    function getScrollHeight() {
        return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
    }


    $(window).scroll(function () {
        if (getScrollTop() + getClientHeight() == getScrollHeight()) {
            get_list(0);
        }
    });
});
    
function PopHide() {
    $("#mask").hide();
    $("#pop").hide();
}
        
function ProductPop(obj) {
    $.ajax({
        url: '/u/goods.html',
        type: 'post',
        dataType: 'json',
        data: {
            'id': obj,
        },
        beforeSend: function () {
        },
        success: function (data) {

            if (data.State == true)
            {
                var d = data.rows;
                var html = "";
                html += ("<div class=\"pop_header\"><p>产品详情</p><img onclick=\"canvasApp('" + d.title + "','" + d.Price + "','" + d.zkPrice + "','" + d.pictUrl + "','" + d.auctionId + "');$('.nb-sharecanvas').css('display', 'flex');\" id=\"pic\" src=\"http://0d077ef9e74d8.cdn.sohucs.com/qnrKblx_png\" style=\"width:24px\"><span onclick='PopHide()'></span></div><div class=\"pop_center\"><div class=\"pop_detail_title clearfix\">");


                html += ("<img src=\"" + d.pictUrl + "\" />");
                html += ("<h6>" + d.title + "</h6></div><div class=\"pop_detail clearfix\">");


                if (d.userType === 0)
                    html += ("<span class=\"tao fl\"></span>");
                else
                    html += ("<span class=\"tianmao fl\"></span>");

                html += ("<p class=\"fl\">" + d.shopTitle + "</p></div><div class=\"pop_detail clearfix\"><p class=\"theme fl\">价格：</p>");
                html += ("<p class=\"fl xian\">￥" + d.Price + "</p>");
                html += ("<p class=\"fl yuan\">￥" + d.zkPrice + "</p></div><div class=\"pop_detail clearfix\">");

                html += ("<p class=\"theme fl\">优惠：</p><p class=\"fl youhui\">￥" + d.couponAmount + "</p></div>");
                html += ("<div class=\"pop_detail clearfix\"><p class=\"theme fl\">佣金：</p>");
                html += ("<p class=\"fl yongjin\">￥" + d.Rate.toFixed(2) + "</p>");
                html += ("<p class=\"bili\">（收入比率<b>" + d.tkRate + "%</b>）</p></div>");

                html += ("<div class=\"pop_detail clearfix\">");
                html += ("<a href=\"" + d.auctionUrl + "\" target=\"_blank\" class=\"fl j_btn red\">商品链接</a>");
                html += ("<a class=\"fl j_btn green\" id='wxtg' onclick='tuiguang(1," + obj + ")'>微信推广</a>");
                html += ("<a class=\"fl j_btn green\" id='qqtg' onclick='tuiguang(2," + obj + ")'>QQ推广</a>");
                html += ("<a class=\"fl j_btn gray\" onclick='copy(this)' id='copyid' >复制文案</a>");
                html += ("<a class=\"fl j_btn waming\" onclick='zhuanfa(" + d.auctionId + ")'  id='zhuanfa' >转发到群</a>");
                html += ("</div>");
                html += ("<div class=\"pop_detail clearfix\">");
                html += ("<textarea class='taodetail'>" + d.title +
        "\n券后【"+ d.Price+"元】包邮秒杀"+
        "\n领券：https://uland.taobao.com/coupon/edetail?tj1=1&tj2=1&activityId="+d.couponActivityId+"&itemId="+d.auctionId+"&dx=1"
        +"\n下单："+d.auctionUrl+
        "\n"+d.Introduce+"</textarea>");
                html += ("</div><div class=\"pop_detail clearfix\"><p></p></div></div>");
                $("#pop").html(html);
                $("#mask").show();
                $("#pop").show();
            }
        }
    });
}


function tuiguang(id, pid) {
    $(".taodetail").val("正在生成推广信息.................");
    $("#wxtg").remove();
    $("#qqtg").remove();
    $("#wxtg").remove();
    $("#qqtg").remove();
    $.ajax({
        url: '/u/kl.html',
        type: 'post',
        data: {
            'id': id,
            'pid': pid
        },
               
        success: function (data) {
            if (data.date != null) {
                $(".waming").show();
                $(".taodetail").val(data.date);
            }
        }
    });
}
function zhuanfa(auctionId)
{
    $.ajax({
        url: '/agentcenter/ApplyZhuanfa',
        type: 'post',
        data: {
            auctionId: auctionId
        },
        success: function (data) {
            if (data.State) {

                alert("转发成功")

                $('#zhuanfa').hide();
            }
        }
    });

}

function copy(obj) {

    $(obj).addClass("btn-success");
    $(obj).addClass("btn-success");
    $(obj).html("已复制");
    var clipboard = new Clipboard('#copyid', {
        text: function (trigger) {
            return $(".taodetail").val();
        }
    });

    setTimeout(function () {
        $(obj).removeClass("btn-success");
        $(obj).html("复制文案");
    }, 1500);
}


     
function canvasApp(title, p1, p2,imgurl,id)
{
           
    repic = imgurl;
    reurl = "/admin/QRImg.html?url=http://" + window.location.host + "/" + $("#aid").val() + "/goods/" + id + ".html";
   // reurl = "/u/QR.html";
    var canvas = document.getElementById('sharecanvas');
    var ctx = canvas.getContext("2d");
    ctx.fillStyle = "#fff";
    canvas.width = (document.documentElement.clientWidth * 0.9) * 2;
    canvas.height = canvas.width / 2 + 680;
    var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
    for (var i = 0; i < imageData.data.length; i += 4) {
        if (imageData.data[i + 3] == 0) {
            imageData.data[i] = 255;
            imageData.data[i + 1] = 255;
            imageData.data[i + 2] = 255;
            imageData.data[i + 3] = 255;
        }
    }
    ctx.putImageData(imageData, 0, 0);
    var item_img = new Image();
    item_img.setAttribute('crossOrigin', 'anonymous');
    item_img.src = repic;
    item_img.onerror = function () {
        $('.image_loading').hide();
        $('.image_show').hide();
        $('.nb-sharecanvas').hide();
        return false;
    }
    item_img.onload = function () {
        ctx.drawImage(item_img, 0, 0, canvas.width, canvas.width);
        var code_img = new Image()
        code_img.setAttribute('crossOrigin', 'anonymous');

        code_img.src = reurl;
        code_img.onerror = function () {
            $('.image_loading').hide();
            $('.image_show').hide();
            $('.nb-sharecanvas').hide();
            return false;
        }
        code_img.onload = function () {
            ctx.drawImage(code_img, canvas.width - 220, canvas.width + 20, 200, 200);
            var str = title;
            ctx.fillStyle = 'inherit';
            ctx.lineWidth = 1;
            ctx.textAlign = 'left';
            ctx.textBaseline = "top";
            ctx.font = '30px Helvetica';
            var lineWidth = 0;
            var canvasWidth = canvas.width - 60;
            var initHeight = canvas.width + 30;
            var lastSubStrIndex = 0;
            for (var i = 0; i <= str.length; i++) {
                lineWidth += ctx.measureText(str[i]).width;
                if (lineWidth > canvasWidth - 230) {
                    ctx.fillText(str.substring(lastSubStrIndex, i), 20, initHeight);
                    initHeight += 40;
                    lineWidth = 0;
                    lastSubStrIndex = i;
                }
                if (i == str.length - 1) {
                    ctx.fillText(str.substring(lastSubStrIndex, i + 1), 20, initHeight);
                }
            }
            var price = "原价：" + p2;
            ctx.fillStyle = '#757380;';
            ctx.textAlign = 'left';
            ctx.font = '30px Helvetica';
            ctx.textBaseline = "top";
            ctx.fillText(price, 20, initHeight + 30);
            ctx.strokeStyle = '#757380;';
            ctx.lineWidth = 2;
            ctx.moveTo(20, initHeight + 46);
            ctx.lineTo(200, initHeight + 46);
            ctx.stroke();

            var end_price_title = "券后价:";

            ctx.font = "bold 30px Arial"
            ctx.fillStyle = "#FF3C29"
            ctx.textAlign = 'left';
            //ctx.font = '30px Helvetica';
            ctx.textBaseline = "top";
            ctx.fillText(end_price_title, 20, initHeight + 80);
            var end_price = p1;
            ctx.fillStyle = '#FF3C29';
            ctx.textAlign = 'left';
            ctx.font = '42px Helvetica';
            ctx.textBaseline = "top";
            ctx.fillText(end_price, 120, initHeight + 72);
            ctx.setLineDash([10, 5]);
            ctx.lineWidth = 1;
            ctx.strokeStyle = '#aba0ac';
            ctx.beginPath();
            ctx.moveTo(10, canvas.height - 50);
            ctx.lineTo(canvas.width - 10, canvas.height - 50);
            ctx.stroke();
            ctx.fillStyle = 'white'
            ctx.lineWidth = 0;
            ctx.beginPath();
            ctx.moveTo((canvas.width - 308) / 2 - 10, canvas.height - 64);
            ctx.lineTo((canvas.width - 308) / 2 + 320, canvas.height - 64);
            ctx.lineTo((canvas.width - 308) / 2 + 320, canvas.height - 46);
            ctx.lineTo((canvas.width - 308) / 2 - 10, canvas.height - 46);
            ctx.lineTo((canvas.width - 308) / 2 - 10, canvas.height - 64);
            ctx.fill();
            var code_title = '长按二维码识别查看商品';
            ctx.fillStyle = '#808080';
            ctx.font = '28px Helvetica';
            ctx.fillText(code_title, (canvas.width - 308) / 2, canvas.height - 64);
            var image = new Image();
            image.setAttribute('crossOrigin', 'anonymous');
            image.src = canvas.toDataURL("img/jpeg");
            $(".imgData").attr("background", "white");
            $(".imgData").attr("src", image.src);
            $(".imgData").show();

            if ($('.imgData').attr('src') != "") {
                $('.image_loading').hide();
                $('.image_show').show();
                $('.am-icon-close').show();
            }
        }
    }
}
